// pages/user/page/userInfo/cn/cn.js
// pages/mine/setting/CN/CN.js
const app = getApp();
const db = wx.cloud.database()
const _ = db.command
const userinfoDB=db.collection('userlogin')
let nickname=null

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // nickname:app.globalData.nickname,
    userPhone:null,
    userName:null,
  },
//  formorPageLoad: function (){
//         var pages = getCurrentPages();
//         var beforePage = pages[pages.length - 2];
//         beforePage.onLoad()
//    },
  userNameInput(event){
    nickname=event.detail.value
    console.log(nickname)
  },
  UDPUserName(e){
    let that=this
    if(nickname == ""){
      wx.showToast({
        title: '昵称不能为空',
        icon:'none'
      })
    }else{
      console.log(that.data.userPhone,that.data.userName)
      wx.cloud.callFunction({
        name:'update',
        data:{
          phone:that.data.userPhone,
          name:nickname,
        },success: res => {
          wx.showToast({  
            title: '修改成功',  
            icon: 'success',  
            duration: 2000  
        })   
          console.log(res)
          app.userDate.userInfo.userName=nickname
          app.formorPageLoad()
          wx.navigateBack({
            url: '../../userInfo/userInfo'
        })
        },
        fail: err => {
          console.log(err)
        }
      })
  }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   //保留其他页面 跳转 带返回按钮
   console.log(options)
   let that=this
   that.setData({
    userPhone:options.num,
    userName:options.name
   })
   nickname = options.name
  },

})